﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
namespace ZF.WS.Admin
{
    public partial class MemberManage : ZF.BLL.Page.MMPage
    {
        //public override HouseEdit HouseEdit { get { return this.HouseEdit1; } }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                    Add();
                else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }
        public void InitControls()
        {
            //this.cblBaseFacility.DataSource = DictionaryConfig.HBaseFacility;
            //this.cblBaseFacility.DataBind();
            //this.cblWithFacility.DataSource = DictionaryConfig.HWithFacility;
            //this.cblWithFacility.DataBind();
            //this.rblDecoration.DataSource = DictionaryConfig.HDecoration;
            //this.rblDecoration.DataBind();
            //this.ddlDirection.DataSource = DictionaryConfig.HDirection;
            //this.ddlDirection.DataBind();
            //this.ddlHouseType.DataSource = DictionaryConfig.HHouseType;
            //this.ddlHouseType.DataBind();
            //this.ddlProperty.DataSource = DictionaryConfig.HProperty;
            //this.ddlProperty.DataBind();

            //var dt = HouseEditPage.DealType.ToString();
            //foreach (Control c in this.Controls)
            //{
            //    if (c is HtmlControl)
            //    {
            //        var hc = (HtmlControl)c;
            //        if (!string.IsNullOrEmpty(hc.Attributes["key"]))
            //        {
            //            hc.Visible = hc.Attributes["key"].Split(',').Contains(dt);
            //        }

            //    }
            //}
            //if (HouseEditPage.DealType == 2)
            //{
            //    this.cbHZ.Checked = true;
            //}
            //HouseEditPage.Type = (HouseEditPage.OperType == PageOperType.AddNew ? "发布" : "修改") + SiteInfo.GetHouseTitle(HouseEditPage.DealType);

        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {


            using (var dc = SiteInfo.CreateDBDataContext())
            {

                var member = dc.US_Member.FirstOrDefault(p => p.Account == this.tbUserName.Text.Trim());
                if (member != null)
                    throw WarnFactory.CreateWarn("该用户名已经存在，请使用其他用户名", WarnIcon.Error, WarnLink.GoBack);
                member = dc.US_Member.FirstOrDefault(p => p.Email == this.tbEmail.Text.Trim());
                if (member != null)
                    throw WarnFactory.CreateWarn("该邮箱已经被注册使用，请使用其他邮箱", WarnIcon.Error, WarnLink.GoBack);
                member = new ZF.Model.US_Member()
                {
                    Account = this.tbUserName.Text.Trim(),
                    Email = this.tbEmail.Text.Trim(),
                    Pwd = SiteInfo.EncryptBase64( this.tbPassword.Text),
                    Address = this.tbAddress.Text.EmptyString(),
                    AddTime = DateTime.Now,
                    Answer = this.tbAnswer.Text.EmptyString(),
                    Contact = this.tbContact.Text.EmptyString(),
                    IDCard = this.tbIDCard.Text.EmptyString(),
                    Phone = this.tbPhone.Text.EmptyString(),
                    Professional = this.ddlJob.SelectedValue.EmptyString(),
                    QQ = this.tbQQ.Text.EmptyString(),
                    Question = this.tbQuestion.Text.EmptyString(),
                    Sex = this.rblSex.SelectedValue.Convert<int>(),
                    Tel = this.tbTel.Text.EmptyString(),
                    Zipcode = this.tbZipcode.Text.EmptyString(),
                    Birthday = string.Format("{0}年{1}月{2}日", Request.Form["year"].EmptyString("-"),
                    Request.Form["month"].EmptyString("-"), Request.Form["day"].EmptyString("-")),
                    Status = 1,
                    Type = 100,
                };

                var success = false;
                try
                {
                     success = SiteInfo.RegisterMember(member,null);
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }
                if (success)
                {
                    string msgKey = "系统管理员";

                    SiteInfo.InsertLog("添加" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success,
                        string.Format("{0}/member/Memberlist.aspx?mt=100", AppKey.SiteKey.ManageDirectory)).Alert();
                }

            }

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                //var member = SiteInfo.GetCurrentMember();
                //if (member == null)
                //    throw WarnFactory.CreateWarn(WarnCode.LoginTimeOut);
                //var house = dc.H_House.FirstOrDefault(p => p.HouseId == HouseEditPage.HouseId && p.MemberId == member.MemberId);
                //if (house == null)
                //    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                //HouseEditPage.DealType = house.DealType;
                //InitControls(); ;

                //this.tbCity.Text = house.AreaId1.ToString();
                //this.tbArea.Text = house.AreaId2.ToString();
                //this.tbAddress.Text = house.Address;
                //this.tbAge.Text = house.Age;
                //this.cblBaseFacility.SelectedValues = house.BaseFacility.Split(',');
                //this.tbContact.Text = house.Contact;
                //this.tbCutNum.Text = string.Concat(house.CutNum);
                //this.rblForm.SelectedValue = house.Form.ToString();
                //this.tbQQ.Text = house.QQ;
                //this.rblDecoration.SelectedValue = house.Decoration;
                //this.tbTitle.Text = house.Title;
                //this.ddlDirection.SelectedValue = house.Direction;
                //this.ddlHXChu.SelectedValue = house.HX_Chu.ToString();
                //this.ddlHXFan.SelectedValue = house.HX_Fan.ToString();
                //this.ddlHXTin.SelectedValue = house.HX_Tin.ToString();
                //this.ddlHXWei.SelectedValue = house.HX_Wei.ToString();
                //this.ddlHXYan.SelectedValue = house.HX_Yan.ToString();
                //this.ddlHouseType.SelectedValue = house.HouseType.ToString();
                //this.cblWithFacility.SelectedValues = house.WithFacility.EmptyString("");
                //this.tbTopNum.Text = string.Concat(house.TopNum);
                //this.tbPhone.Text = house.Phone;
                //this.ddlPeriod.SelectedValue = house.Period;
                //this.tbDescription.Text = house.Description;
                //this.tbTraffic.Text = house.Traffic;
                //this.tbUserArea.Text = string.Concat(house.UserArea);
                //this.tbConstructArea.Text = string.Concat(house.ConstructArea);
                //this.tbEngage.Text = house.Engage;
                //this.tbPubEmail.Text = house.PubEmail;
                //this.tbPayment.Text = house.Payment;
                //this.tbPhoto.Text = house.Photo;
                //this.lvImgList.DataSource = house.Photo.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                //    .Select(p => new
                //    {
                //        File = p
                //    });
                //this.lvImgList.DataBind();
                //if (house.DealType >= 5 && house.DealType <= 8 && house.H_Sell != null)
                //{
                //    this.tbUnitPrice.Text = string.Concat(house.H_Sell.UnitPrice);
                //    this.tbPreferential.Text = house.H_Sell.Preferential;
                //    this.tbSFPrice.Text = house.Price.ToString();
                //    this.ddlProperty.SelectedValue = house.H_Sell.Property;
                //}
                //else if (house.DealType >= 1 && house.DealType <= 4 && house.H_Rent != null)
                //{
                //    if (house.DealType == 2)
                //        this.cbHZ.Checked = true;
                //    var rent = house.H_Rent;
                //    this.tbEnterTime.Text = rent.EnterTime;
                //    this.rblHZ_ISex.SelectedValue = rent.HZ_ISex.ToString();
                //    this.rblHZ_RSex.SelectedValue = rent.HZ_RSex.ToString();
                //    this.tbHZDescription.Text = rent.HZDescription;
                //    this.ddlLeastNum.SelectedValue = rent.LeastNum.ToString();
                //    this.tbZFPrice.Text = house.Price.ToString();

                //}




            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    //var house = dc.H_House.FirstOrDefault(p => p.HouseId == HouseEditPage.HouseId);
                    //if (house == null)
                    //    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                    //house.AreaId1 = this.tbCity.Text.Convert<int>();
                    //house.AreaId2 = this.tbArea.Text.EmptyInt();
                    //house.Address = this.tbAddress.Text;
                    //house.AddTime = DateTime.Now;
                    //house.Age = this.tbAge.Text;
                    //house.BaseFacility = this.cblBaseFacility.SelectedValues.ConstituteString();
                    //house.Contact = this.tbContact.Text;
                    //house.CutNum = this.tbCutNum.Text;
                    //house.Form = this.rblForm.SelectedValue.Convert<int>();
                    //house.QQ = this.tbQQ.Text;
                    //house.Decoration = this.rblDecoration.SelectedValue;
                    //house.Title = this.tbTitle.Text;
                    //house.Direction = this.ddlDirection.SelectedValue;
                    //house.HX_Chu = this.ddlHXChu.SelectedValue.Convert<int>();
                    //house.HX_Fan = this.ddlHXFan.SelectedValue.Convert<int>();
                    //house.HX_Tin = this.ddlHXTin.SelectedValue.Convert<int>();
                    //house.HX_Wei = this.ddlHXWei.SelectedValue.Convert<int>();
                    //house.HX_Yan = this.ddlHXYan.SelectedValue.Convert<int>();
                    //house.HouseType = this.ddlHouseType.SelectedValue;
                    //house.WithFacility = this.cblWithFacility.SelectedValues.ConstituteString();
                    //house.TopNum = this.tbTopNum.Text;
                    //house.Phone = this.tbPhone.Text;
                    //house.Period = this.ddlPeriod.SelectedValue;
                    //house.Description = this.tbDescription.Text;
                    //house.Traffic = this.tbTraffic.Text;
                    //house.UserArea = this.tbUserArea.Text;
                    //house.ConstructArea = this.tbConstructArea.Text;
                    //house.Engage = this.tbEngage.Text;
                    //house.PubEmail = this.tbPubEmail.Text;
                    //house.Status = 1;
                    //house.MemberId = SiteInfo.GetCurrentMember() != null ? SiteInfo.GetCurrentMember().MemberId : 0;
                    //house.IsMemberPub = SiteInfo.GetCurrentMember() != null;
                    //house.IsVerify = false;
                    //var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    //photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                    //house.Photo = photoList.ConstituteString(";"); ;
                    //house.Payment = this.tbPayment.Text;
                    //if (house.DealType == 3 || house.DealType == 4)//如果是售房
                    //{
                    //    house.Price = this.tbSFPrice.Text.Convert<int>();
                    //    var sell = house.H_Sell;
                    //    sell.UnitPrice = this.tbUnitPrice.Text.EmptyInt();
                    //    sell.Preferential = this.tbPreferential.Text;
                    //    sell.Property = this.ddlProperty.SelectedValue;
                    //}
                    //else if (house.DealType == 1 || house.DealType == 2)//如果是租房 
                    //{
                    //    house.DealType = this.cbHZ.Checked ? 2 : 1;
                    //    house.Price = this.tbZFPrice.Text.Convert<int>();
                    //    var rent = house.H_Rent;
                    //    rent.EnterTime = this.tbEnterTime.Text.EmptyString();
                    //    rent.HZ_ISex = this.rblHZ_ISex.SelectedValue.Convert<int>();
                    //    rent.HZ_RSex = this.rblHZ_RSex.SelectedValue.Convert<int>();

                    //    rent.HZDescription = this.tbHZDescription.Text.EmptyString();
                    //    rent.LeastNum = this.ddlLeastNum.SelectedValue.Convert<int>();
                    //}
                    //else
                    //{
                    //    throw new Exception();
                    //}






                    //try
                    //{
                    //    dc.SubmitChanges();
                    //    ts.Complete();
                    //}
                    //catch (Exception ex)
                    //{
                    //    throw WarnFactory.CreateWarn(ex);
                    //}
                    //string msgKey = SiteInfo.GetHouseTitle(house.DealType);

                    //SiteInfo.InsertLog("修改" + msgKey, null);
                    //WarnMessageLink link = new WarnMessageLink(string.Format("成功修改一个{0}，点击确定按钮返回{0}列表", msgKey));
                    //link.AddWarnMessageLink(string.Format("查看该{0}", msgKey),
                    //    string.Format("~/House/HouseInfo.aspx?hid={0}", house.HouseId));
                    //link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey),
                    //    Request.PathInfo);
                    //WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, "/person/HouseList.aspx").Alert();







                }


            }

        }

    
    }
}
